<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
    :disabled="disabled"
  >
    <el-table :data="formData" show-summary :summary-method="getSummaries" class="-mt-10px">
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="销售单号" min-width="200">
        <template #default="{ row }">
          <el-form-item class="mb-0px!">
            <el-input disabled v-model="row.no" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="产品金额" prop="productPrice" fixed="right" min-width="100">
        <template #default="{ row }">
          <el-form-item class="mb-0px!">
            <el-input disabled v-model="row.productPrice" :formatter="erpPriceInputFormatter" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="税额" prop="taxPrice" fixed="right" min-width="100">
        <template #default="{ row }">
          <el-form-item class="mb-0px!">
            <el-input disabled v-model="row.taxPrice" :formatter="erpPriceInputFormatter" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="应付金额" prop="needReceiptPrice" fixed="right" min-width="100">
        <template #default="{ row }">
          <el-form-item class="mb-0px!">
            <el-input disabled v-model="row.needReceiptPrice" :formatter="erpPriceInputFormatter" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="已付金额" prop="receiptedPrice" fixed="right" min-width="100">
        <template #default="{ row }">
          <el-form-item class="mb-0px!">
            <el-input disabled v-model="row.receiptedPrice" :formatter="erpPriceInputFormatter" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="本次收款" prop="receiptPrice" fixed="right" min-width="115">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.receiptPrice`" class="mb-0px!">
            <el-input-number
              v-model="row.receiptPrice"
              controls-position="right"
              :precision="2"
              class="!w-100%"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" class="mb-0px!">
            <el-input v-model="row.remark" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column> -->
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3" v-if="!disabled">
    <el-button @click="handleOpenSaleOut" round>+ 添加销售单</el-button>
    <!-- <el-button @click="handleOpenSaleReturn" round>+ 添加销售退货单</el-button> -->
  </el-row>

  <!-- 可收款的【销售出库单】列表 -->
  <SaleOutReceiptEnableList ref="saleOutReceiptEnableListRef" @success="handleAddSaleOut" />
  <!-- 可收款的【销售出库单】列表 -->
  <!-- <SaleReturnRefundEnableList ref="saleReturnRefundEnableListRef" @success="handleAddSaleReturn" /> -->
</template>
<script setup lang="ts">
// import { ProductVO } from '@/api/erp/product/product'
import { erpPriceInputFormatter, getSumValue } from '@/utils'
import SaleOutReceiptEnableList from '@/views/erp/sale/out/components/SaleOutReceiptEnableList.vue'
// import SaleReturnRefundEnableList from '@/views/erp/sale/return/components/SaleReturnRefundEnableList.vue'
import { SaleOutVO } from '@/api/erp/sale/out'
import type { FormInstance } from 'element-plus'
// import { ErpBizType } from '@/utils/constants'
// import { SaleReturnVO } from '@/api/erp/sale/return'

const props = defineProps<{
  items?: ReceiptItem[]
  customerId?: number
  disabled?: boolean
}>()
const message = useMessage()

const formLoading = ref(false) // 表单的加载中

// 定义收款项的接口
interface ReceiptItem {
  id: number
  no: string
  productPrice: number
  taxPrice: number
  needReceiptPrice: number
  receiptedPrice: number
  receiptPrice: number
  remark: string
}

const formData = ref<ReceiptItem[]>([]) // 使用明确的类型注解
const formRules = reactive({
  receiptPrice: [{ required: true, message: '本次收款不能为空', trigger: 'blur' }]
})
const formRef = ref<FormInstance>() // 表单 Ref
// const productList = ref<ProductVO[]>([]) // 产品列表

/** 初始化设置出库项 */
watch(
  () => props.items,
  async (val) => {
    formData.value = val || []
  },
  { immediate: true }
)

/** 合计 */
const getSummaries = (param: { columns: any[]; data: any[] }) => {
  const { columns, data } = param
  const sums: string[] = []
  columns.forEach((column, index: number) => {
    if (index === 0) {
      sums[index] = '合计'
      return
    }
    if (
      ['productPrice', 'taxPrice', 'needReceiptPrice', 'receiptedPrice', 'receiptPrice'].includes(
        column.property
      )
    ) {
      const sum = getSumValue(data.map((item) => Number(item[column.property])))
      sums[index] = erpPriceInputFormatter(sum)
    } else {
      sums[index] = ''
    }
  })
  return sums
}

/** 新增【销售出库】按钮操作 */
const saleOutReceiptEnableListRef = ref()
const handleOpenSaleOut = () => {
  if (!props.customerId) {
    message.error('请选择客户')
    return
  }
  saleOutReceiptEnableListRef.value.open(props.customerId)
}
const handleAddSaleOut = (rows: SaleOutVO[]) => {
  rows.forEach((row) => {
    formData.value.push({
      id: row.id,
      no: row.no,
      productPrice: row.totalProductPrice,
      taxPrice: row.totalTaxPrice,
      needReceiptPrice: row.totalPrice,
      receiptedPrice: row.receiptedPrice,
      receiptPrice: row.totalPrice - row.receiptedPrice,
      remark: '' // 备注字段，可选
    })
  })
}

/** 新增【销售退货】按钮操作 */
// const saleReturnRefundEnableListRef = ref()
// const handleOpenSaleReturn = () => {
//   if (!props.customerId) {
//     message.error('请选择客户')
//     return
//   }
//   saleReturnRefundEnableListRef.value.open(props.customerId)
// }
// const handleAddSaleReturn = (rows: SaleReturnVO[]) => {
//   rows.forEach((row) => {
//     formData.value.push({
//       bizId: row.id,
//       bizType: ErpBizType.SALE_RETURN,
//       bizNo: row.no,
//       totalPrice: -row.totalPrice,
//       receiptedPrice: -row.refundPrice,
//       receiptPrice: -row.totalPrice + row.refundPrice,
//       remark: '', // 备注字段，可选
//       // 添加API文档中要求的其他字段
//       receiptTime: new Date().getTime(), // 收款时间，使用当前时间作为默认值
//       customerName: '', // 客户名称，需要从父组件获取
//       totalNeedReceiptPrice: -row.totalPrice, // 应收金额
//       totalReceiptedPrice: -row.refundPrice, // 已收金额
//       totalReceiptPrice: -row.totalPrice + row.refundPrice // 本次收款金额
//     })
//   })
// }

/** 删除按钮操作 */
const handleDelete = (index: number) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value?.validate()
}
defineExpose({ validate })
</script>
